package com.itfreer.content.task;

import android.annotation.SuppressLint;
import android.content.Intent;
import android.graphics.Color;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.view.Gravity;
import android.view.View;
import android.view.ViewGroup;
import android.widget.LinearLayout;
import android.widget.ScrollView;

import androidx.annotation.NonNull;

import com.itfreer.content.R;
import com.itfreer.content.task.workSitutation.WorkSitutationDTO;
import com.itfreer.content.task.workSitutation.WorkSitutationService;
import com.itfreer.content.task.workSitutation.WorkSitutationVO;
import com.itfreer.core.collection.SerializableMap;
import com.itfreer.core.volley.callback.DataCallback;
import com.itfreer.ui.activity.FormActivity;
import com.itfreer.ui.base.TextView;

import java.lang.reflect.Field;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import jsc.kit.wheel.dialog.DateTimeWheelDialog;

public class TaskTotalActivity extends FormActivity {

    private LinearLayout totalForm;

    private float scale;

    private Handler handler;

    private TextView curYear;
    private LinearLayout yearLayout;

    @SuppressLint("HandlerLeak")
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_task_total);

        scale = this.getResources().getDisplayMetrics().density;

        // 返回
        TextView back =(TextView)findViewById(R.id.taskTotalBack);
        back.setOnClickListener(new View.OnClickListener() {
            public void onClick(View v) {
                finish();
            }
        });

        totalForm = findViewById(R.id.taskTotalForm);
        curYear = findViewById(R.id.curYear);
        yearLayout = findViewById(R.id.selectYear);

        yearLayout.setOnClickListener(new View.OnClickListener() {
            public void onClick(View v) {
                showTimeDialog(1);
            }
        });
        /**
         * 接收解析后传过来的数据
         */
        handler = new Handler() {
            @Override
            public void handleMessage(Message msg) {
                List<WorkSitutationVO> model = (List<WorkSitutationVO>) msg.obj;
                loadTotal(model);
            }
        };
        // 打开时同步一次
        new Thread(new Runnable() {
            @Override
            public void run() {
                loadData(handler);
            }
        }).start();

    }

    private void loadData(final Handler handler) {
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy");
        String year = sdf.format(new Date());
        List<WorkSitutationVO> result= queryData(year);
        handler.sendMessage(handler.obtainMessage(22, result));
    }

    private void loadTotal(List<WorkSitutationVO> result) {
        System.out.println("111");
        totalForm.removeAllViews();

        if(result != null && result.size() > 0) {
            for(int i=0;i<result.size();i++) {
                WorkSitutationVO work = result.get(i);
                LinearLayout layout = new LinearLayout(TaskTotalActivity.this);
                LinearLayout.LayoutParams layoutParams = new LinearLayout.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.WRAP_CONTENT);
                layoutParams.topMargin = (int) (4 * scale + 0.5f);
                layout.setLayoutParams(layoutParams);
                layout.setOrientation(LinearLayout.HORIZONTAL);

                layout.addView(getText(work.getDate()!=null?work.getDate():""));
                layout.addView(getText(work.getLocuscount()!=null?work.getLocuscount().toString():""));
                layout.addView(getText(work.getDistance()!=null?work.getDistance().toString():""));
                layout.addView(getText(work.getLocusrate()!=null?work.getLocusrate().toString():""));
                totalForm.addView(layout);
            }
        } else {
            LinearLayout layout = new LinearLayout(TaskTotalActivity.this);
            LinearLayout.LayoutParams layoutParams = new LinearLayout.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.WRAP_CONTENT);
            layoutParams.topMargin = (int) (4 * scale + 0.5f);
            layout.setLayoutParams(layoutParams);
            layout.setOrientation(LinearLayout.HORIZONTAL);

            layout.addView(getText("暂无数据"));
            totalForm.addView(layout);
        }
    }

    private TextView getText(String text) {
        TextView textView = new TextView(TaskTotalActivity.this);
        LinearLayout.LayoutParams textViewParams = new LinearLayout.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.WRAP_CONTENT);
        textViewParams.weight = 1;
        textView.setLayoutParams(textViewParams);
        textView.setTextColor(Color.parseColor("#4a4a4a"));
        textView.setGravity(Gravity.CENTER);
        textView.setTextSize((6 * scale + 0.5f));
        textView.setText(text);
        return textView;
    }

    private DateTimeWheelDialog showTimeDialog(int type) {
        Calendar calendar = Calendar.getInstance();
        calendar.set(Calendar.YEAR, 2000);
        calendar.set(Calendar.MONTH, 0);
        calendar.set(Calendar.DAY_OF_MONTH, 1);
        calendar.set(Calendar.HOUR_OF_DAY, 0);
        calendar.set(Calendar.MINUTE, 0);
        Date startDate = calendar.getTime();
        calendar = Calendar.getInstance();
        calendar.set(Calendar.YEAR, 2100);
        Date endDate = calendar.getTime();

        DateTimeWheelDialog dialog = new DateTimeWheelDialog(TaskTotalActivity.this);
        dialog.show();
        dialog.setTitle("选择时间");
        int config = DateTimeWheelDialog.SHOW_YEAR_MONTH_DAY_HOUR_MINUTE;
        switch (type) {
            case 1://只显示年
                config = DateTimeWheelDialog.SHOW_YEAR;
                break;
            case 2://显示年、月
                config = DateTimeWheelDialog.SHOW_YEAR_MONTH;
                break;
            case 3://显示年、月、日
                config = DateTimeWheelDialog.SHOW_YEAR_MONTH_DAY;
                break;
            case 4://显示年、月、日、时
                config = DateTimeWheelDialog.SHOW_YEAR_MONTH_DAY_HOUR;
                break;
            case 5://显示年、月、日、时、分
                config = DateTimeWheelDialog.SHOW_YEAR_MONTH_DAY_HOUR_MINUTE;
                break;
        }
        dialog.configShowUI(config);
        dialog.setCancelButton("取消", null);
        dialog.setOKButton("确定", new DateTimeWheelDialog.OnClickCallBack() {
            @Override
            public boolean callBack(View v, @NonNull Date selectedDate) {
                SimpleDateFormat sdf = new SimpleDateFormat("yyyy");
                String year = sdf.format(selectedDate);
                List<WorkSitutationVO> result= queryData(year);
                loadTotal(result);
                return false;
            }
        });
        dialog.setDateArea(startDate, endDate, true);
        dialog.updateSelectedDate(new Date());
        return dialog;
    }

    private List<WorkSitutationVO> queryData(String year) {
        curYear.setText(year);
        Calendar calendar = Calendar.getInstance();
        calendar.set(Calendar.YEAR, Integer.parseInt(year));
        calendar.set(Calendar.MONTH, 0);
        calendar.set(Calendar.DAY_OF_MONTH, 1);
        calendar.set(Calendar.HOUR_OF_DAY, 0);
        calendar.set(Calendar.MINUTE, 0);
        calendar.set(Calendar.SECOND, 0);
        Date startTime = calendar.getTime();
        calendar = Calendar.getInstance();
        calendar.set(Calendar.MONTH, 11);
        calendar.set(Calendar.DAY_OF_MONTH, 31);
        calendar.set(Calendar.HOUR_OF_DAY, 23);
        calendar.set(Calendar.MINUTE, 59);
        calendar.set(Calendar.SECOND, 59);
        Date endTime = calendar.getTime();
        WorkSitutationDTO work = new WorkSitutationDTO();
        work.setStartTime(startTime);
        work.setEndTime(endTime);
        List<WorkSitutationVO> result = WorkSitutationService.instance.getDataSituationByState(work);
        return result;
    }
}
